
//使用future来获取异步线程的执行结果
#include <iostream>
#include <future>
#include <chrono>


int Add(int num1,int num2)
{
    std::this_thread::sleep_for(std::chrono::seconds(3));
    std::cout<<"开始执行！"<<std::endl;
    int resul=num1+num2;
    return resul;
}
int main()
{
    //std::launch::deferred  当调用get获取执行结果时，异步线程才会执行任务
    //std::launch::async 异步线程会立即执行任务
    // std::future<int> ret=std::async(std::launch::deferred,Add,11,22);
    std::future<int> ret=std::async(std::launch::async,Add,11,22);
    std::this_thread::sleep_for(std::chrono::seconds(1));
    int sum=ret.get();//获取执行结果
    std::cout<<sum<<std::endl;
    return 0;
}